########################################################
###### Replication Code for Figure 1 and Table A5 ######
################## January 9th, 2025 ################### 
########################################################

rm(list=ls())

######## Load Data
library(fixest)
library(dplyr)
library(modelsummary)
library(gt)
library(ggplot2)

df <- readRDS("data/workfile.rds")

###### Treatments effects by condition
df <- df %>% 
  group_by(dem_country_code) %>% 
  mutate(trust_in_opposing_parties2 = scale(trust_in_opposing_parties))
df$trust_in_opposing_parties <- scale(df$trust_in_opposing_parties)

coef_df <- data.frame()

trust_no_cov <- feols(trust_in_opposing_parties ~ treated_econ + treated_culture, df, se = "iid") %>% 
  summary()
trust_cts_fe <- feols(trust_in_opposing_parties ~ treated_econ + treated_culture  | dem_country_code, df, se = "iid") %>% 
  summary()
trust_cov_adj <- feols(trust_in_opposing_parties ~ treated_econ + treated_culture + 
                         mean_outparty_feeling + high_pol_interest | dem_country_code, df, se = "iid") %>% 
  summary()

coef_df <- data.frame(coef = c(trust_no_cov$coefficients[2:3], trust_cts_fe$coefficients[1:2], trust_cov_adj$coefficients[1:2]), 
                      se = c(trust_no_cov$se[2:3], trust_cts_fe$se[1:2], trust_cov_adj$se[1:2]),
                      name = rep(c("Economic issues \n condition", "Cultural issues \n condition"), times = 3),
                      var = rep(c("No covariates", "Country-FE", "Country-FE + covariate adj."), each = 2))

pd <- position_dodge(0.5)

coef_df <- coef_df

##### Figure 1

regs <- ggplot(coef_df, aes(x = name,y = coef, color = var)) + 
  ylim(c(-0.15, 0.05)) +
  geom_point(position = pd, shape = 21, fill = 'white') + 
  geom_errorbar(aes( ymin = coef - se*1.959964, ymax = coef + se*1.959964), width = 0, position = pd) + 
  geom_errorbar(aes( ymin = coef - se*1.644854, ymax = coef + se*1.644854), width = 0, position = pd, linewidth = 1.5) + 
  geom_hline(yintercept = 0, color = "red", linetype = "dashed") +
  scale_colour_manual(values = c("black", "grey", "darkorange")) + 
  xlab("") + 
  ylab("Treatment effect on out-partisan trust") +
  coord_flip() +
  #facet_wrap(~ var) + 
  theme_bw() + 
  guides(color=guide_legend(title=""))
regs

ggsave("figures/Fig1_agg_treatments.pdf", 
       height = 4, width = 6)


##### Table A5

modellist <- list(trust_no_cov, trust_cts_fe, trust_cov_adj)
modelsummary(modellist, stars = T)
modelsummary(modellist, stars = T, 
             title = "Treatment effect on out-partisan trust",
             statistic = c("std.error",
                           "p = {p.value}"),
             gof_omit = "BIC|AIC|r2.within|r2.within.adjusted|rmse|std.error.type|se_type",
             coef_rename = c("treated_econ" = "Economic issues dimension", 
                             "treated_culture" = "Cultural issues dimension", 
                             "mean_outparty_feeling" = "Average outpartisan feeling", 
                             "high_pol_interest" = "High political interest"),
             add_rows = data.frame(t(c("Country-FE included", "No", "Yes", "Yes"))),
             output = "gt") %>% 
  gtsave("tables/TabA5_aggregate_treatment.docx")


